import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../ddialog/dialog.dart';
import 'dialog.dart';

class ToastUtils {
  static void showError(String text) {
//    show(text, icon: "drive_images/icon_auto_alarm.png");
    show(text,);
  }

  static void showSuccess(String text) {
//    show(text, icon: "drive_images/icon_success.png");
    show(text,);
  }

  static Timer? _timer;
  static Function? _timerCall;
  static late BuildContext context;

  static void show(String text, {String? icon}) {
    _timer?.cancel();
    if (_timerCall != null) {
      _timerCall!();
      _timerCall = null;
    }

    String type = "toast";


    DDialogUtils.showToastDialog(
        context, Toast(text: text, icon: icon), onDispose: () {
      _timerCall = null;
      _timer?.cancel();
    }, backgroundColor: Color(0x00000000));
    _timerCall = () {
      Navigator.of(context).pop();
    };
    _timer = new Timer(Duration(milliseconds: 1500), () {
      if (_timerCall != null) {
        _timerCall!();
      }
      _timerCall = null;
    });
  }

  static void dismiss({String type = "toast"}) {
    DialogUtils.dismissCenterDialog(type: type);
  }
}

///视图
class Toast extends StatelessWidget {
  final String? text;
  final String? icon;
  final Color textColor = Color(0xff3D3636);
  final Color backgroundColor = Colors.white;

  Toast({this.text, this.icon});

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Container(
        constraints: BoxConstraints(minWidth: 104),
        padding: EdgeInsets.only(left: 16, right: 16, top: 12, bottom: 12),

        decoration: BoxDecoration(
          boxShadow: [
            BoxShadow(color: Colors.black.withOpacity(0.08),offset: Offset(0,6), blurRadius: 16),
            BoxShadow(color: Colors.black.withOpacity(0.05),offset: Offset(0,9), blurRadius: 28),
            BoxShadow(color: Colors.black.withOpacity(0.03),offset: Offset(0,12), blurRadius: 48),
          ],
          color: backgroundColor, borderRadius: BorderRadius.circular(10),
          border: Border.all(color: Color(0xffE3E3E3)),),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            icon == null
                ? SizedBox(
              width: 1,
              height: 1,
            )
                : Padding(
              padding: const EdgeInsets.only(bottom: 4),
              child: ImageIcon(
                AssetImage(icon!),
                size: 28,
                color: textColor,
              ),
            ),
            Text("${text}",
                style: TextStyle(
                  inherit: false,
                  fontSize: 14,
                  color: textColor,
                )),
          ],
        ),
      ),
    );
  }
}
